/**
 * Created by uptain on 2017/8/15.
 */
const houseId = fxj.getSearch().get('houseId');
const ja = new Vue({
    el: '#main',
    data: {
        introduce: 1, //介绍页
        setIntroduce: 0, //介绍页显示
        intro_text: { //介绍页面显示控制
            area1: 0,
            area2: 0,
            area3: 0,
            area4: 0,
        },

        house_describ: { //房源描述数据
            house_desc: '',
            house_communication: '',
            house_surrounding: '',
        },
        cover_src: '',
        text_content: '',
    },
    methods: {
        introduceChild(type) {
            if (fxj.getSearch().get('type') === 'add') {
                clickSaveData();
            }
            this.intro_text.area1 = 0;
            this.intro_text.area2 = 0;
            this.intro_text.area3 = 0;
            this.intro_text.area4 = 0;
            switch (type) {
                case 0:
                    this.introduce = 1;
                    this.setIntroduce = 0;
                    return;
                    break;
                case 2:
                    this.intro_text.area2 = 1;
                    this.text_content = '    请介绍一下您的房子特色或者您想分享给房客的东西，让房客更好地了解您和您的家。\n例如：\n毗邻宽窄巷子的小两居，在这里你可以感受到历史文化的古朴气息，热闹与宁静并存。一杯茶，一本书，偷得浮生几时闲。若你我有缘，路过请停下脚步，我有酒，你有歌吗？';
                    break;

                case 3:
                    this.intro_text.area3 = 1;
                    this.text_content = '详细描述通过什么交通方式怎样到达您的家。\n例如：\n涌泉西站(西)，向前步行100米到天来国际广场；地铁4号线到地铁涌泉站D口出，向前步行200米到天来国际广场。';
                    break;

                case 4:
                    this.intro_text.area4 = 1;
                    this.text_content = '介绍一下房源周边情况，包括景区、商业街、超市、学校、医院等。\n例如：\n保安24小时值班，住宿安全。区内绿化覆盖，无噪音影响，宜晚饭后散步。小区周边有红旗连锁、家乐福超市，肯德基、德克士快餐店，夜市小吃一条街，步行200米到万达广场，500米到人民医院，1公里到大学城。';
                    break;
            }
            this.introduce = 0;
            this.setIntroduce = 1;
        },

        async send_house_describ() {
            let ok = true;
            const tips = [
                ['house_desc', '请填写房源介绍'],
                ['house_communication', '请填写交通路线'],
                ['house_surrounding', '请填写周边情况'],
            ];
            forEach(tips, tip => {
                if (!isExist(ja.house_describ[tip[0]])) {
                    ok = false;
                    toast(tip[1]);
                }
            });
            if (!ok) return;
            const close = fxj.dialog.loading({noBg: true});
            const re = await ajax({
                url: `home/${fxj.getSearch()
                    .get('type') === 'add' ? 'houseUpload/describeAdd' : 'houseManage/houseUpdateDescribe'}`,
                data: {
                    houseId,
                    'house_desc': ja.house_describ.house_desc,
                    'house_communication': ja.house_describ.house_communication,
                    'house_surrounding': ja.house_describ.house_surrounding,
                },
            });
            close();
            if (re.err === 0) {
                if (fxj.getSearch().get('type') === 'add') {
                    localStorage.removeItem('house_desc');
                    localStorage.removeItem('house_communication');
                    localStorage.removeItem('house_surrounding');
                }
                await toast('提交成功!');
                if (fxj.getSearch().get('papa') === 'menu') {
                    history.back();
                }
                else {
                    goPage('hm_picture.html', {
                        data: {
                            houseId,
                            type: fxj.getSearch().get('type'),
                        },
                    });
                }
            }
            else {
                toast(re.msg);
            }
        },
    },
});

async function created() {
    if (fxj.getSearch().get('type') === 'add') return;
    const re = await ajax({
        url: 'home/houseDetail/getHouseDetail',
        data: {
            houseId,
        },
    });
    if (re.err === 0) {
        const {introduction, transportation, environment} = re.data;
        ja.house_describ.house_desc = introduction;
        ja.house_describ.house_communication = transportation;
        ja.house_describ.house_surrounding = environment;
    }
}

if (fxj.getSearch().get('type') === 'add') {
    ja.house_describ.house_desc = localStorage.getItem('house_desc') || ja.house_describ.house_desc;
    ja.house_describ.house_communication = localStorage.getItem('house_communication') || ja.house_describ.house_communication;
    ja.house_describ.house_surrounding = localStorage.getItem('house_surrounding') || ja.house_describ.house_surrounding;
}

function apiready() {
    created();
}


function backPage() {
    if (fxj.getSearch().get('papa') === 'menu') {
        localStorage.removeItem('house_desc');
        localStorage.removeItem('house_communication');
        localStorage.removeItem('house_surrounding');
        location.replace(`hm_menu.html?houseId=${houseId}&type=alter`);
    }
    else {
        closeToWin();
    }
}

function clickSaveData() {
    localStorage.setItem('house_desc', ja.house_describ.house_desc);
    localStorage.setItem('house_communication', ja.house_describ.house_communication);
    localStorage.setItem('house_surrounding', ja.house_describ.house_surrounding);
}